var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();

var start01 = 0;
var start02 = 0;
var start = 0;
Page(Object.assign({}, Zan.Tab, Zan.TopTips, {

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      list: [{
        id: 0,
        title: '已申请'
      }, {
        id: 1,
        title: '已到账'
      }],
      selectedId: 0,
      height: 40,
      scroll: false
    },
    record01: [],
    record02: [],
    activeIndex: 0,
    recordsTotal01: 0,
    recordsTotal02: 0,
    nodata: {
      msg: '没有记录',
      notype: 1,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    },
    userinfo: {},
    expIntoDateStr: 15,
    value2: 1

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.data.tab.selectedId = options.index;
    this.setData({
      tab: this.data.tab,
    })
    this.setData({
      activeIndex: options.index
    })

    this.onPullDownRefresh();

    if (app.globalData.userId) {
      this.setData({
        userinfo: app.globalData.userAndTenantInfo
      })
    } else {
      // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userAndTenantInfoCallback = res => {
        this.setData({
          userinfo: app.globalData.userAndTenantInfo
        })

      }
    }
    // this.setData({
    //   expIntoDateStr: options.expIntoDateStr,
    //   value2: options.value2,
    //   dqyongjin: options.dqyongjin
    // })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    //status  1申请 0到账
    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        record01: []
      })
      network._post_page_json(this, api.withdrawalsRecord, start01, { status: 1 }, success1);
    } else {
      start02 = 0;
      this.setData({
        record02: []
      })
      network._post_page_json(this, api.withdrawalsRecord, start02, { status: 2 }, success2);
    }
    

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      [`${componentId}.selectedId`]: selectedId
    })

    this.setData({
      activeIndex: selectedId
    })

    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        record01: []
      })
      network._post_page_json(this, api.withdrawalsRecord, start01, { status: 1 }, success1);
    } else {
      start02 = 0;
      this.setData({
        record02: []
      })
      network._post_page_json(this, api.withdrawalsRecord, start02, { status: 2 }, success2);
    }

  }

}))

// 提现到由记录
var success1 = function (that, res) {
  if (res.data.data) {
    var record1 = res.data.data;
    for (var i = 0; i < record1.length; i++) {
      record1[i].createDate = utils.formatDate(new Date(record1[i].createDate));
      // if (record1[i].amount % 100 == 0){
      //   record1[i].amount = record1[i].amount +'.00'
      // }
      
    }
    that.setData({
      record01: that.data.record01.concat(record1),
      recordsTotal: res.data.recordsTotal
    })
  }
}
var success2 = function (that, res) {
  if (res.data.data) {
    var record2 = res.data.data;
    for (var i = 0; i < record2.length; i++) {
      record2[i].createDate = utils.formatDate(new Date(record2[i].createDate));
      
    }
    that.setData({
      record02: that.data.record02.concat(record2),
      recordsTotal: res.data.recordsTotal
    })
  }
}